package cn.vastbase.jdbc.cluster;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * JDBC分布式负载均衡测试
 */
public class ClusterTestCase {

    /**
     * 随机模式负载均衡
     */
    @Test
    public void autoBalance() {
        String url = "jdbc:postgresql://172.16.55.163:5432,172.16.55.164:5432,172.16.55.165:5432/testdb?autoBalance=shuffle";
        for (int i = 0; i < 10; i++) {
            try (Connection connection = DriverManager.getConnection(url, "zxs", "Vastdata#0730");
                 ResultSet rs = connection.createStatement().executeQuery("SELECT inet_server_addr(), inet_server_port()")) {
                if (rs.next()) {
                    String serverAddr = rs.getString(1);
                    int serverPort = rs.getInt(2);
                    System.out.println("Connected to server at: " + serverAddr + ":" + serverPort);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 随机模式负载均衡
     */
    @Test
    public void autoBalance2() {
        String url = "jdbc:postgresql://172.16.55.163:5432,172.16.55.164:5432,172.16.55.165:5432/testdb?loadBalanceHosts=true&targetServerType=any";
        for (int i = 0; i < 10; i++) {
            try (Connection connection = DriverManager.getConnection(url, "zxs", "Vastdata#0730");
                 ResultSet rs = connection.createStatement().executeQuery("SELECT inet_server_addr(), inet_server_port()")) {
                if (rs.next()) {
                    String serverAddr = rs.getString(1);
                    int serverPort = rs.getInt(2);
                    System.out.println("Connected to server at: " + serverAddr + ":" + serverPort);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 读写分离，随机读
     */
    @Test
    public void readWriteSplit() {
        String url = "jdbc:postgresql://172.16.55.163:5432,172.16.55.164:5432,172.16.55.165:5432/testdb?loadBalanceHosts=true&targetServerType=master";
        List<Connection> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            try {
                Connection connection = DriverManager.getConnection(url, "zxs", "Vastdata#0730");
                connection.setAutoCommit(false);
                list.add(connection);
                Statement statement = connection.createStatement();
                statement.execute("insert into users (username) values ('ssss')");
                try (ResultSet rs = statement.executeQuery("SELECT inet_server_addr(), inet_server_port()")) {
                    if (rs.next()) {
                        String serverAddr = rs.getString(1);
                        int serverPort = rs.getInt(2);
                        System.out.println("Connected to server at: " + serverAddr + ":" + serverPort);
                    }
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                for (Connection connection : list) {
                    try {
                        connection.close();
                    } catch (SQLException ignored) {
                    }
                }
            }
        }
    }
}
